package cn.xxm.ff.log.logback;

import ch.qos.logback.classic.pattern.MessageConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import cn.xxm.ff.log.common.utils.PropertyUtil;
import cn.xxm.ff.log.core.service.DesensitizationPolicy;
import cn.xxm.ff.log.core.service.DesensitizationPolicyFactory;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class SensitiveDataConverter extends MessageConverter {
    private Map<String, Set<String>> map = new HashMap(0);
    private boolean stringKillSwitch;
    private boolean switchClose;
    private String[] filterPackages = new String[0];
    private String desensitizeType = "json_improvement";
    private DesensitizationPolicy desensitizationPolicy;

    public SensitiveDataConverter() {
        this.map = PropertyUtil.getTypeFieldsMapping();
        Map<String, String> configMap = PropertyUtil.getGlobalConfig();
        this.stringKillSwitch = StringUtils.equalsIgnoreCase((String)configMap.get("stringKillSwitch"), "true");
        this.switchClose = StringUtils.equalsIgnoreCase((String)configMap.get("closeSwitch"), "true");
        this.desensitizeType = (String)configMap.getOrDefault("strategy", "json_improvement");
        this.desensitizationPolicy = DesensitizationPolicyFactory.getPolicy(this.desensitizeType);
        String packages = (String)configMap.get("packages");
        this.filterPackages = StringUtils.split(packages, ",");
    }

    private void extractConfigs() {
        boolean killSwitch = StringUtils.equalsIgnoreCase((String)PropertyUtil.getGlobalConfig().get("stringKillSwitch"), "true");
        boolean log4jopenSwitch = StringUtils.equalsIgnoreCase((String)PropertyUtil.getGlobalConfig().get("closeSwitch"), "true");
        String desensitizeType = (String)PropertyUtil.getGlobalConfig().getOrDefault("strategy", "json_improvement");
        String packages = (String)PropertyUtil.getGlobalConfig().get("packages");
        String[] filterPackages = StringUtils.split(packages, ",");
        if (killSwitch != this.stringKillSwitch) {
            this.stringKillSwitch = killSwitch;
        }

        if (log4jopenSwitch != this.switchClose) {
            this.switchClose = log4jopenSwitch;
        }

        if (StringUtils.equals(desensitizeType, this.desensitizeType)) {
            this.desensitizationPolicy = DesensitizationPolicyFactory.getPolicy(desensitizeType);
        }

        this.filterPackages = filterPackages;
    }

    public String convert(ILoggingEvent source) {
        long begin = System.currentTimeMillis();
        if (!this.switchClose && PropertyUtil.isReady()) {
            this.extractConfigs();
            String msg = source.getMessage();
            if (msg == null) {
                return source.getFormattedMessage();
            } else if (this.checkIfFilterLog(source)) {
                return source.getFormattedMessage();
            } else {
                String formattedMessage = source.getFormattedMessage();
                if (StringUtils.isNotBlank(formattedMessage)) {
                    int beginIndex = formattedMessage.indexOf("{");
                    if (beginIndex == -1) {
                        return this.handleNoDesensitizationMessage(source);
                    }

                    int endIndex = formattedMessage.lastIndexOf("}");
                    if (endIndex == -1 || endIndex <= beginIndex) {
                        return this.handleNoDesensitizationMessage(source);
                    }

                    try {
                        formattedMessage = this.desensitizationPolicy.desensitizeFormattedMessage(formattedMessage, this.map);
                    } catch (Exception var9) {
                        return this.handleNoDesensitizationMessage(source);
                    }
                }

                return formattedMessage;
            }
        } else {
            return source.getFormattedMessage();
        }
    }

    public String handleNoDesensitizationMessage(ILoggingEvent source) {
        return this.stringKillSwitch ? "*******`******" : source.getFormattedMessage();
    }

    public boolean checkIfFilterLog(ILoggingEvent source) {
        if (this.filterPackages != null && this.filterPackages.length != 0) {
            String loggerName = source.getLoggerName();
            String[] var3 = this.filterPackages;
            int var4 = var3.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String packName = var3[var5];
                if (StringUtils.isNotBlank(loggerName) && loggerName.startsWith(packName)) {
                    return true;
                }
            }

            return false;
        } else {
            return false;
        }
    }

    
}
